package com.hrzyit.whistle.cases.ui.adapter

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hrzyit.base.ui.adapter.BaseRecyclerViewAdapter
import com.hrzyit.base.utils.DateUtils
import com.hrzyit.base.utils.IMUtils
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.CaseInfoEntity
import kotlinx.android.synthetic.main.activity_case_list.view.*

/**
 * 案件列表 Adapter
 * @author YangWei
 * @date 2019/3/29
 */
class CaseAdapter(context: Context) : BaseRecyclerViewAdapter<CaseInfoEntity, CaseAdapter.ViewHolder>(context) {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext)
            .inflate(R.layout.activity_case_list, parent, false)

        return ViewHolder(view)

    }

    /**
     * 绑定视图组件数据及事件
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        super.onBindViewHolder(holder, position)

        val model = dataList[position]

        // 根据案件处理流程状态绘制边框颜色
        val backgroundDrawable = ContextCompat.getDrawable(mContext,R.drawable.case_list_border_)
        val gradientDrawable: GradientDrawable = backgroundDrawable as GradientDrawable
        var borderColorResId = R.color.common_white
        when(model.status) {
            2 -> {
                // 吹哨
                borderColorResId = R.color.common_red_dark
            }
            5 -> {
                // 结案
                borderColorResId = R.color.common_green
            }
            else -> {
                when(model.isOverTime) {
                    1 -> {
                        // 超时
                        borderColorResId = R.color.common_red_dark
                    }
                    else -> {
                        // 办理中
                        borderColorResId = R.color.common_yellow
                    }
                }
            }
        }
        val borderColor = ContextCompat.getColor(mContext,borderColorResId)
        gradientDrawable.setStroke(IMUtils.dp2px(mContext,1.00f),borderColor)
        holder.itemView.mCaseItemList.background = gradientDrawable

        // 任务单号
        holder.itemView.mCaseNumberLv.setContentText(model.taskNumber!!)
        // 上报时间
        val reportTime = DateUtils.convertTimeToString(
            model.addTime!!.toLong(), "yyyy-MM-dd HH:mm:ss"
        )
        holder.itemView.mAddTimeLv.setContentText(reportTime)
        // 案件类别
        holder.itemView.mCaseTypeTv.text = model.typeName
        // 大类名称
        holder.itemView.mOneSortTv.text = model.oneSortName
        // 小类名称
        holder.itemView.mSecondSortTv.text = model.secondSortName
        // 案件地址
        holder.itemView.mCaseAddressTv.text = model.address
        // 案件描述
        holder.itemView.mDescriptionTv.text = model.description

        // 切换显示是否批示图标，默认未批示
        val isAuth = if (model.isAuth == null) {
            0
        } else {
            model.isAuth
        }
        // 显示批示或未批示图标
        var authDrawableId: Int = if (isAuth == 1) {
            R.drawable.anjianhuizong_pic_yps
        } else {
            R.drawable.anjianhuizong_pic_dps
        }

        var drawable = ContextCompat.getDrawable(mContext, authDrawableId)

        holder.itemView.mLeaderApproveIv.setImageDrawable(drawable)

        // 判断案件分类显示不同图标:=1 网格案件 ; =2 12345案件;3=转批案件;4=吹哨案件;5=超时案件
        val caseSort = model.caseSort
        var iconDrawableId = 1
        when (caseSort) {
            1 -> {
                // =1 网格案件
                iconDrawableId = R.drawable.anjianhuizong_icon_xc
            }
            2 -> {
                // =2 12345案件
                iconDrawableId = R.drawable.anjianhuizong_icon_ts
            }
            3 -> {
                // 3=转批案件
                iconDrawableId = R.drawable.anjianhuizong_icon_pz
            }
            4 -> {
                // 4=吹哨案件
                iconDrawableId = R.drawable.anjianhuizong_icon_cs
            }
            5 -> {
                // 5=超时案件
                iconDrawableId = R.drawable.anjianhuizong_icon_cs_1
            }
        }


        var iconDrawable = ContextCompat.getDrawable(mContext, iconDrawableId)
        holder.itemView.mCaseTypeIconIv.setImageDrawable(iconDrawable)

        // 注册查看批示事件
        holder.itemView.mLeaderApproveIv.setOnClickListener {
            mItemClickListener!!.onItemClick(it, model, position)
        }

        // 注册查看案件详情事件
        holder.itemView.mCaseItemList.setOnClickListener {
            mItemClickListener!!.onItemClick(it, model, position)
        }
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}